package start;
import java.io.*;
import java.net.*;
import java.util.*;

import servers.StorageServerThread;

import xchange.Debug;

/**
 * This class implements a separate application (server process)
 * handles the GET, POST and REMOVE commands
 * blocks are stored in ./files directory
 */

public class StorageServer {

    private String ip = "";
    private final int port = 9002;
    private final int BLOCKSIZE = 64*1024; 

    ServerSocket ss;
 
    public StorageServer() {
	if (Debug.DEBUG) {
	    System.out.println("Storageserver started ...");
	}
	// check if ./files directory exists
	File file = new File("./files");
	if (!file.exists()) {
	    System.out.println("Please create directory ./files with r+w access.");
	    System.err.println("ERROR : directory ./files does not exist.");
	    System.exit(0);
	}
	
	try {
		ss = new ServerSocket(port);
	} catch (IOException e1) {
		e1.printStackTrace();
	}
	
	while(true){
		  StorageServerThread sst;
		  try{
			  sst = new StorageServerThread(ss.accept());
		      Thread t = new Thread(sst);
		      t.start();
		  }
		  catch(IOException e) {
		        System.out.print(e);
		    }
		}
    }
   
    public static void main(String[] args) throws Exception {
        new StorageServer();
    }
}
